<?php

/**
 * TeacherController
 * 
 * @author liumingl
 */

require_once 'Zend/Controller/Action.php';

class TeacherController extends Zend_Controller_Action 
{
	protected $_teacherModel;
	
	public function init()
	{
		$this->_teacherModel = new Eleven_Model_Teacher();
		
		$this->view->publishForm = $this->getPublishForm();
		
		$this->view->isGongTong = true;
		$this->view->isTeacher = true;
	}
	/**
	 * The default action - show the home page
	 */
	public function indexAction()
	{
	    $currentTopicId = $this->_request->getParam('topicId',null);
	    
		$this->view->currentTopic = $this->_teacherModel->getCurrentTopic($currentTopicId);
		$this->view->isCurrentTopic = $this->_teacherModel->isCurrentTopic($currentTopicId);
		
		$this->view->currentContents = $this->_teacherModel->getCurrentContents($this->_getParam('topicId'), $this->_getParam('page', 1));
	}
    
	public function viewAction()
	{
	    $content = $this->_teacherModel
				->getContentById($this->_getParam('contentId'));
        
        if (null === $content) {
            throw new CMS_Exception_404('未知的文档， Teacher Content文档Id为 ' . $this->_getParam('contentId'));
        }else{
        	$content->views++;
        	$content->save();
        }
        
        $replys = $this->_teacherModel
                ->getReplysByContentId($this->_getParam('contentId'),$this->_getParam ('replyPageId',1),'id ASC');
        
        $this->view->assign(array(
            'content'   => $content,
            'replys'	=> $replys,
            'replyForm' => $this->getReplyForm($this->_getParam('contentId')),
            )
        );
        
	}
	
    public function getReplyForm($id)
	{
		$urlHelper = $this->_helper->getHelper('url');

        $this->_forms['reply'] = $this->_teacherModel->getForm('teacherReply');
        $this->_forms['reply']->setAction($urlHelper->url(array(
            'controller' => 'teacher',
            'action' => 'savereply'
            ),
            'default'
        ));
        
        $this->_forms['reply']->setMethod('post');
        $this->_forms['reply']->getElement('contentId')->setValue($id);

        return $this->_forms['reply'];
	}
	
    public function getPublishForm( )
	{
		$urlHelper = $this->_helper->getHelper('url');

        $this->_forms['publish'] = $this->_teacherModel->getForm('teacherPublish');
        $this->_forms['publish']->setAction($urlHelper->url(array(
            'controller' => 'teacher',
            'action' => 'save'
            ),
            'default'
        ));
        
        $this->_forms['publish']->setMethod('post');
        
        return $this->_forms['publish'];
	}
	
    public function savereplyAction()
	{
		$request = $this->getRequest();
		if (!$request->isPost()) 
		{
			return $this->_helper->redirector('index');
  		}
  		
  		if (false === $this->_teacherModel->saveReply($request->getPost())) 
  		{
  		    $content = $this->_teacherModel
				->getContentById($request->getPost('contentId'));
            
			$replys = $this->_teacherModel
                ->getReplysByContentId($this->_getParam('contentId'),$this->_getParam ('replyPageId',1),'id ASC');
                
            $this->view->assign(array(
                'content' => $content,
                'replys'	=> $replys,
                'replyForm' => $this->getReplyForm($this->_getParam('contentId')),
                )
            );
            return $this->render('view');
  		}
  		
  		$url = $this->_helper->getHelper('Url');
        $redirect = $this->_helper->getHelper('Redirector');
  		return $redirect->gotoUrl($url->url(array('contentId' => $request->getPost('contentId')), 'teacher_content'));
	}
	
	public function publishAction()
	{
	    if (!$this->_teacherModel->checkAcl('publish')) {
            return $this->_helper->redirector('index','teacher');
        }
	}
	
	public function listtopicAction()
	{
		$topics = $this->_teacherModel->getTopics ( $this->_getParam ( 'page', 1 ), array ('id DESC' ) );
		
		$this->view->assign(array('topics'=>$topics));
	}
	
	public function saveAction()
	{
	    if (!$this->_teacherModel->checkAcl('save')) {
            return $this->_helper->redirector('index','teacher');
        }
        
	    $request = $this->getRequest();
	    
		if (!$request->isPost()) 
		{
			return $this->_helper->redirector('index');
  		}

  		if (false === $this->_teacherModel->savePost($request->getPost(),$_FILES['file'])) 
  		{
     		return $this->render('publish');
  		}
  		
  		return $this->_helper->redirector('index');
	}
}

